#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <openssl/sha.h>
typedef struct
{
    uint32_t timeLow;
    uint16_t timeMid;
    uint16_t timeHiAndVersion;
    uint8_t clockSeqAndNode[8];
} UUID;
typedef struct
{
    uint8_t b[16];
} uuid_t;

#define UUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7)                            \
    ((uuid_t){{((a) >> 24) & 0xff, ((a) >> 16) & 0xff, ((a) >> 8) & 0xff, (a) & 0xff, \
               ((b) >> 8) & 0xff, (b) & 0xff,                                         \
               ((c) >> 8) & 0xff, (c) & 0xff,                                         \
               (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7)}})

static const UUID uuid = {0xa09cac58, 0x8620, 0x8346, {0xa1, 0xb8, 0xec, 0x4b, 0xc0, 0x8e, 0x01, 0xb6}};
static const uuid_t tee_client_uuid_ns = UUID_INIT(0x58ac9ca0, 0x2086, 0x4683, 0xa1, 0xb8, 0xec, 0x4b, 0xc0, 0x8e, 0x01, 0xb6);

void print_uuid(const unsigned char *uuid)
{
    // 假设 uuid 是一个 16 字节的数组
    char uuid_str[37]; // UUID 格式字符串的长度：8-4-4-4-12 + 4 个 '-'

    // 使用 sprintf 格式化 UUID 字节数组
    snprintf(uuid_str, sizeof(uuid_str),
             "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
             uuid[0], uuid[1], uuid[2], uuid[3],
             uuid[4], uuid[5], uuid[6], uuid[7],
             uuid[8], uuid[9], uuid[10], uuid[11],
             uuid[12], uuid[13], uuid[14], uuid[15]);

    // 使用 printf 打印格式化后的 UUID 字符串
    printf("%s\n", uuid_str);
}

void print_hex(const uint8_t *buf, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        printf("%02x", buf[i]);
    }
    printf("\n");
}


int main()
{
    uint8_t obuf[20] = {0};
    print_uuid(tee_client_uuid_ns.b);
    print_uuid(&uuid);


    SHA1(tee_client_uuid_ns.b, sizeof(tee_client_uuid_ns), obuf);
    print_hex(obuf,20);

    const char *path = "/usr/bin/optee_example_api_test";
    const char path_sha1[41] = "b2f387657609fda2d4628bb3c63825c383653bb9";
    // SHA1(path,strlen(path),obuf);
    print_hex(&uuid,16);
    struct 
    {
        char uuid[16];
        char path[41];
    } ibuf;
    memcpy(ibuf.uuid,&uuid,16);
    print_hex(ibuf.uuid,16);
    memcpy(ibuf.path,path_sha1,41);
    SHA1(&ibuf, sizeof(ibuf), obuf);
    print_hex(obuf,20);
}